package com.xxx.rabbitmq.controller;

import com.xxx.rabbitmq.config.SnowflakeIdWorker;
import com.xxx.rabbitmq.enums.RabbitStatusEnum;
import com.xxx.rabbitmq.enums.RedisKeyEnum;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

/**
 * 生产者
 *
 * @author
 * @DATE 2025/5/31
 **/
@RestController
@Slf4j
public class RabbitMQProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 批量发送消息
     *
     * @param message
     */
    @RequestMapping("/sendQueueBatch")
    public void sendQueueBatch(String message) {
        //请求头设置消息id（messageId）
        Map<String, Object> map = new HashMap<>();
        map.put("message", message);
        for (int i = 0; i < 3; i++) {
            long id = snowflakeIdWorker.nextId();
            map.put("id", id);
            JSONObject entries = JSONUtil.parseObj(map);
            redisTemplate.opsForValue().set(RedisKeyEnum.MQ_STATUS.getKey() + id, RabbitStatusEnum.CONSUME.getCode());
            rabbitTemplate.convertAndSend("direct.exchange", "direct.key", entries);
        }
        log.info("3个消息都发送成功");
    }
}
